package com.yz.lzq.liwushuo.ui.modules.home.nomal;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.yz.lzq.liwushuo.R;
import com.yz.lzq.liwushuo.decoration.SpacesItemDecoration;
import com.yz.lzq.liwushuo.jsonbean.home.boutique.HomeBoutiqueRecycleBean;
import com.yz.lzq.liwushuo.ui.modules.comminterface.OnMyItemClickListener;
import com.yz.lzq.liwushuo.ui.webview.home.HomeWebContentActivity;
import com.yz.lzq.liwushuo.utils.ActivityUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/11/12.
 */

public class HomeNomalFragment extends Fragment implements HomeNomalContract.HomeNomalViewInterface {
    @Bind(R.id.recyvleView_Home_nomal)
    XRecyclerView recyvleViewHomeNomal;
    /* @Bind(R.id.imageView_emptyView)
     ImageView imageViewEmptyView;
     @Bind(R.id.relativeLayout_emptyView)
     RelativeLayout relativeLayoutEmptyView;*/
    private Context mContext = null;
    private int fragmentId;
    private HomeNomalPresenter nomalPresenter = null;
    private HomeNomalFragmentRecycleViewAdapter recycleViewAdapter = null;
    private boolean isRefreshing = false;
    private boolean isLoadingMore = false;
    List<HomeBoutiqueRecycleBean.DataBean.ItemsBean> mList = new ArrayList<>();


    /**
     * 初始化Homefragment对象的单例模式
     *
     * @return 当前fragment对象
     */
    public static HomeNomalFragment getInstance(int id) {
        HomeNomalFragment fragment = new HomeNomalFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("id", id);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        fragmentId = arguments.getInt("id");
    }

    /**
     * @param inflater           布局加载器
     * @param container          父容器
     * @param savedInstanceState 保存状态的信息
     * @return 返回加载布局
     */
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home_nomal, null);
        ButterKnife.bind(this, view);
        return view;
    }


    /**
     * 为控件赋值
     *
     * @param savedInstanceState 获取保存状态的信息
     */
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        initEmptyView();
        LinearLayoutManager manager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        recyvleViewHomeNomal.setLayoutManager(manager);
        recyvleViewHomeNomal.addItemDecoration(new SpacesItemDecoration(5));

        recyvleViewHomeNomal.setHasFixedSize(true);
        nomalPresenter = new HomeNomalPresenter(mContext, this);
        nomalPresenter.showRecycleView(false);
        recyvleViewHomeNomal.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                isRefreshing = !isRefreshing;
                nomalPresenter.showRecycleView(false);
                recyvleViewHomeNomal.refreshComplete();
            }

            @Override
            public void onLoadMore() {
                isLoadingMore = !isLoadingMore;
                nomalPresenter.showRecycleView(true);
                recyvleViewHomeNomal.loadMoreComplete();
            }
        });
        recycleViewAdapter = new HomeNomalFragmentRecycleViewAdapter(mContext, mList, new OnMyItemClickListener() {
            @Override
            public void clickMyItem(View view, int position) {
                if (null != mList.get(position).getUrl()) {
                    ActivityUtil.startWebViewActivity(mContext, HomeWebContentActivity.class, new String[]{mList.get(position).getUrl()});
                }
            }
        });
        recyvleViewHomeNomal.setAdapter(recycleViewAdapter);
    }


    /**
     * 视图销毁
     */
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    /**
     * 碎片销毁
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    @Override
    public void getHomeJingPinRecycleBean(HomeBoutiqueRecycleBean recycleBean) {
      /*  currentRecycleBean = recycleBean;
        initEmptyView();*/
        if (mList.size() == 0 && null != recycleBean) {
            mList.addAll(recycleBean.getData().getItems());
            recycleViewAdapter.reloadRecyclerView(recycleBean.getData().getItems(), true);
        } else if (isRefreshing) {
            mList.clear();
            mList.addAll(recycleBean.getData().getItems());
            recycleViewAdapter.reloadRecyclerView(mList, true);
            isRefreshing = !isRefreshing;
        } else if (isLoadingMore) {
            mList.addAll(recycleBean.getData().getItems());
            recycleViewAdapter.reloadRecyclerView(mList, true);
            isLoadingMore = !isLoadingMore;
        }
    }

    @Override
    public int getFragmentID() {
        return fragmentId;
    }
}
